<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/4/11
 * Time: 20:29
 */

namespace console\controllers;
use common\models\Comments;
use common\models\UpFileData;
use common\models\CommentsReply;
use common\models\CommentsRelation;

use Yii;

class MigrateBestryController extends \yii\console\Controller{


    /*
     * 优选评论数据迁移,优选goods_id,product_id不变，喜试product_no, , 加2000W
     * php yii migrate-bestry/index 1242222 10000
     * */

    public function actionIndex($start, $num){


        $comment_id = $start;
        for(; $comment_id < $start+$num; $comment_id++) {

            /*
             * 查询优选的 gcomment_table、gcomment_query表
             * */
            $comment = Yii::$app->db->createCommand('SELECT * FROM tmall1.gcomment_table a  JOIN tmall1.gcomment_query b ON a.uid = b.comment_id WHERE a.uid = ' . $comment_id);
            $commentData = $comment->queryOne();

            if (!empty($commentData)) {

                $add_time = $commentData['addtime'] == 0 ? '2008-01-01 08:00:00' : date('Y-m-d h:i:s', $commentData['addtime']);
                $imgStr = '';

                for($i=1;$i<=4;$i++) {//图片地址处理
                    if (!empty($commentData['img' . $i])) {

                        $img = explode('./', $commentData['img' . $i]);
                        if (count($img) > 1) {
                            $imgPath = $img[1];
                        } else {
                            $imgPath = $img[0];
                        }
                        $imgP = explode('upload/', $imgPath);
                        if(count($imgP) > 1){//去掉图片地址字段里的upload/
                            $imgPath = $imgP[1];
                        }
                        $tile = explode('/', $imgPath);
                        $tileName = $tile[count($tile) - 1];
                        //var_dump($imgPath);die;
                        $imgArr = array(
                            'c_uri' => $imgPath,
                            'uid' => $commentData['member_uid'],
                            'db_time' => $add_time,
                            'title' => $tileName,
                        );

                        $upImg = new UpFileData($imgArr);
                        $upImg->save();
                        if(empty($imgStr)){
                            $imgStr .= $upImg->id;
                        }else{
                            $imgStr .= ','.$upImg->id;

                        }
                    }

                }

                //填入comments表的数据
                $commentNewData = array(
                    'comment_id' => $comment_id,
                    'content' => $commentData['comment_main'],
                    'create_time' => $add_time,
                    'img'   => $imgStr,
                );
                //var_dump($commentNewData);

                $modelC = Comments::findOne($comment_id);
                if(empty($modelC)){

                    $modelS = new Comments($commentNewData);
                    $modelS->save();

                }
                else{

                    $modelC->load($commentNewData,'');
                    $modelC->save();
                }


                $reply = Yii::$app->db->createCommand('select * from tmall1.reply_comment WHERE comment_id = ' . $comment_id);
                $replyData = $reply->queryOne();
                $is_reply = 0;
                $reply_id = 0;
                if (!empty($replyData)) {
                    //填入comments_reply表的数据
                    $replyNewData = array(
                        'comment_id' => $replyData['comment_id'],
                        'admin_id' => $replyData['admin_id'],
                        'create_time' => date('Y-m-d h:i:s', $replyData['addtime']),
                        'content' => $replyData['reply_body'],
                        'update_time' => date('Y-m-d h:i:s', $replyData['addtime']),
                    );

                    $replyC = CommentsReply::findOne(['comment_id'=>$comment_id]);
                    if(empty($replyC)){
                        $replyC = new CommentsReply($replyNewData);
                        $replyC->save();
                    }
                    else{
                        $replyC->load($replyNewData, '');
                        $replyC->save();
                    }
                    //var_dump($replyModel->errors);die;
                    $is_reply = 1;
                    $reply_id = $replyC->reply_id;
                }

                //填入comments_relation表的数据
                $level = $commentData['high'] == 1 ? 5 : ($commentData['middle'] == 1 ? 3 : 1);
                $relationNewData = array(
                    //'id' => $relationData['uid'],
                    'comment_id' => $comment_id,
                    'order_id' => $commentData['order_id'],
                    'goods_id' => $commentData['goods_id'],
                    'product_id' => $commentData['product_id'],
                    'bestry_uid' => $commentData['member_uid'],
                    'level' => $level,
                    'create_time' => $add_time,
                    'is_try' => 0,
                    'is_reply' => $is_reply,
                    'reply_id' => $reply_id,
                    'is_show' => $commentData['isshow'],
                    'status' => $commentData['perfect'],
                    'flag'  => $commentData['ftype']
                );

                $relationC = CommentsRelation::findOne(['comment_id'=>$comment_id]);
                if(empty($relationC)){
                    $relationC = new CommentsRelation($relationNewData);
                    $relationC->save();
                }
                else{
                    $relationC->load($relationNewData, '');
                    $relationC->save();
                }

            }

        }

    }

    public function actionImg(){
/*        $imgPath = './upfile/201306/ascdasbncvieln.png';
        $img = explode('./',$imgPath);
        if(is_numeric(strpos($imgPath,'./'))){
            $i = $img[1];
        }
        else
            $i = $img[0];

        $ii = explode('/', $i);
        var_dump(count($ii));
        var_dump($ii[count($ii)-1]);*/


        $commentData['high'] = 0;
        $commentData['middle'] = 0;
        $level = $commentData['high'] == 1 ? 5 : ($commentData['middle'] == 1 ? 3 : 1);
        var_dump($level);
    }

}
